﻿using System;
using System.Collections.Generic;

namespace HtmlParser
{
	struct HtmlTagForRead
	{
		public HtmlTagForRead(HtmlTagCollection parent, string name, HtmlTagType type, int[] position)
		{
			this.type = type;
			this.name = name;
			this.position = position;
			this.parent = parent;
		}
		public HtmlTagType type;
		public string name;
		public int[] position;
		private readonly HtmlTagCollection parent;
		public override string ToString()
		{
			return parent.html.Substring(position[0], position[1] - position[0] + 1);
		}

        public bool IsEmpty
        {
            get
            {
                var s = parent.html.Substring(position[0], position[1] - position[0] + 1);
                return String.IsNullOrWhiteSpace(s);
            }
        }
	}

	class HtmlTagCollection : IList<HtmlTagForRead>
	{
		public HtmlTagCollection(string html)
		{
			this.html = html;
			this.children = new List<HtmlTagForRead>();

		}
		private readonly List<HtmlTagForRead> children;
		public readonly string html;

		public void Add(string name, HtmlTagType type, int[] position)
		{
			this.Add(new HtmlTagForRead(this, name, type, position));
		}

		public void AddText(int[] position)
		{
			Add(null, HtmlTagType.Text, position);
		}
		public void AddTag(string name, int[] position)
		{
			Add(name, HtmlTagType.Tag, position);
		}
		public void AddSpecial(HtmlTagType type, int[] position)
		{
			Add(null, type, position);
		}

		public int IndexOf(HtmlTagForRead item)
		{
			return children.IndexOf(item);
		}

		public void Insert(int index, HtmlTagForRead item)
		{
			children.Insert(index, item);
		}

		public void RemoveAt(int index)
		{
			children.RemoveAt(index);
		}

		public HtmlTagForRead this[int index]
		{
			get
			{
				return children[index];
			}
			set
			{
				children[index] = value;
			}
		}

		public void Add(HtmlTagForRead item)
		{
			children.Add(item);
		}

		public void Clear()
		{
			children.Clear();
		}

		public bool Contains(HtmlTagForRead item)
		{
			return children.Contains(item);
		}

		void ICollection<HtmlTagForRead>.CopyTo(HtmlTagForRead[] array, int arrayIndex)
		{
			children.CopyTo(array, arrayIndex);
		}

		public int Count
		{
			get { return children.Count; }
		}

		bool ICollection<HtmlTagForRead>.IsReadOnly
		{
			get { return false; }
		}

		public bool Remove(HtmlTagForRead item)
		{
			return children.Remove(item);
		}

		IEnumerator<HtmlTagForRead> IEnumerable<HtmlTagForRead>.GetEnumerator()
		{
			return children.GetEnumerator();
		}

		System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
		{
			return children.GetEnumerator();
		}

		public override string ToString()
		{
			return String.Format("Count = {0}", Count);
		}
	}





}
